package com.villaday.idf.custom;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

import com.villaday.idf.R;
import com.villaday.idf.adapter.DateCheckInAdapter;
import com.villaday.idf.model.HousesStates;
import com.villaday.idf.util.StickySectionDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日历控件
 * Created by peng on 2017/8/3.
 */

public class CalendarView extends LinearLayout implements StickySectionDecoration.GroupInfoCallback {
    private final RecyclerView bodyView;
    private DateCheckInAdapter dateCheckInAdapter;

    public CalendarView(Context context) {
        this(context, null);
    }

    public CalendarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CalendarView(final Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        inflate(context, R.layout.layout_calendar_view, this);
        //初始化星期标头
        initWeekGridView(context);
        //月份列表
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(context, 7);
        bodyView = (RecyclerView) findViewById(R.id.bodyView);
        bodyView.setLayoutManager(mGridLayoutManager);
        dateCheckInAdapter = new DateCheckInAdapter(context,null);

        StickySectionDecoration stickySectionDecoration = new StickySectionDecoration(context, this);
        bodyView.addItemDecoration(stickySectionDecoration);
       /* final GridDecoration decoration = new GridDecoration(dateCheckInAdapter.getData().size(), 7) {
            @Override
            public String getHeaderName(int pos) {
                return dateCheckInAdapter.getData().get(pos).getDate() == null ? "" : dateCheckInAdapter.getData().get(pos).getDate().substring(0, 7);
            }
        };

        decoration.setOnHeaderClickListener(new NormalDecoration.OnHeaderClickListener() {
            @Override
            public void headerClick(int pos) {

                Toast.makeText(context, "点击到头部" + pos, Toast.LENGTH_SHORT).show();
            }
        });

        decoration.setOnDecorationHeadDraw(new NormalDecoration.OnDecorationHeadDraw() {
            @Override
            public View getHeaderView(int pos) {
                final View headView = LayoutInflater.from(context).inflate(R.layout.decoration_men_head_view, null);
                TextView tvName = (TextView) headView.findViewById(R.id.header_tv_name);
                tvName.setText(dateCheckInAdapter.getData().get(pos).getDate() == null ? "" : dateCheckInAdapter.getData().get(pos).getDate().substring(0, 7));


                return headView;
            }
        });
        bodyView.addItemDecoration(decoration);*/


       /* GroupListener groupListener = new GroupListener() {
            @Override
            public String getGroupName(int position) {
                return dateCheckInAdapter.getData().get(position).getDate() == null ? "" : dateCheckInAdapter.getData().get(position).getDate().substring(0, 7);
            }
        };

        StickyDecoration decoration = StickyDecoration.Builder
                .init(groupListener,this)
                .setGroupBackground(ContextCompat.getColor(getContext(), R.color.colorWhite))     //背景色
                .setGroupHeight((int) getResources().getDimension(R.dimen.calendar_decoration_height))     //高度
                .setDivideColor(ContextCompat.getColor(getContext(), R.color.month_divide_line_color))           //分割线颜色
                .setDivideHeight((int) getResources().getDimension(R.dimen.calendar_decoration_divide_line_height))  //分割线高度 (默认没有分割线)
                .setGroupTextColor(ContextCompat.getColor(getContext(), R.color.calendar_text_decoration_color))                        //字体颜色
                .setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)) //加粗
                .setGroupTextSize((int) getResources().getDimension(R.dimen.calendar_decoration_text_size))   //字体大小
                .setTextSideMargin(PhoneUtil.dp2px(getContext(), 10))  //边距   靠左时为左边距  靠右时为右边距
                .setTextAlign(Paint.Align.CENTER)                      //居中显示
                .build();
        bodyView.addItemDecoration(decoration);*/
        bodyView.setAdapter(dateCheckInAdapter);

    }

    public void setAdapter(List<HousesStates.BodyEntity.MonthsEntity.ListEntity> adapter) {

        dateCheckInAdapter.setData(adapter);
    }


    private void initWeekGridView(Context context) {
        String[] from = new String[]{"week"};
        int[] to = new int[]{R.id.tvWeekDay};
        String[] strings = new String[]{
                "一", "二", "三", "四", "五", "六", "日",
        };
        List<Map<String, String>> weeks = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            Map<String, String> map = new HashMap<>();
            map.put(from[0], strings[i]);
            weeks.add(map);
        }
        ListAdapter adapter = new SimpleAdapter(context, weeks, R.layout.layout_week_view, from, to);
        GridView weekView = (GridView) findViewById(R.id.weekView);
        weekView.setNumColumns(adapter.getCount());
        weekView.setAdapter(adapter);
        weekView.setSelector(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    public HousesStates.BodyEntity.MonthsEntity.ListEntity getGroupInfo(int position) {
        return dateCheckInAdapter.getData().get(position);
    }
}